<template>
    <div>
        <el-container class="home-container">
            <!-- 头部区域 -->
            <el-header class="el-header">
                <div>
                    <img src="../assets/heima.png" alt="">
                    <span>电商后台管理系统</span>
                </div>
                <el-button @click="loginOut">退出登录</el-button>
            </el-header>
            <el-container>
                <!-- 侧边栏 -->
                <el-aside :width="isCollapse ? '64px' : '200px'" class="el-aside">
                    <div class="toggle-btn" @click="toggleCollapse">|||</div>
                    <!-- 菜单区域 -->
                    <el-menu background-color="#333744" text-color="#fff" 
                    active-text-color="#409bff" :unique-opened='true' :collapse="isCollapse"
                    :collapse-transition = "isCollapse" :router='true' :default-active="activeStatus">
                        <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id"> 
                            <template slot="title">
                                <i :class="iconsObj[item.id]"></i>
                                <span>{{item.authName}}</span>
                            </template>
                            <el-menu-item :index="'/' + item.path" v-for="subItem in item.children" :key="subItem.id"
                            @click="saveNavState('/'+ item.path)">
                                <template slot="title">
                                    <i class="el-icon-location"></i>
                                    <span>{{subItem.authName}}</span>
                                </template>
                            </el-menu-item>
                        </el-submenu>
                    </el-menu>
                </el-aside>
                <!-- 主题区域 -->
                <el-main>
                    <!-- 路由占位符 -->
                    <router-view></router-view>
                </el-main>
            </el-container>
        </el-container>

    </div>
</template>

<script>
import { RouterView } from 'vue-router'

export default {
    data() {
        return {
            menuList: [],
            iconsObj: {
                "125": "iconfont icon-users",
                "103": "iconfont icon-lock_fill",
                "101": "iconfont icon-shangpin",
                "102": "iconfont icon-danju",
                "145": "iconfont icon-baobiao"
            },
            isCollapse: false,
            activeStatus: null
        };
    },
    created() {
        this.getMenuList();
        this.activeStatus = window.sessionStorage.getItem('activeStatus')
    },
    methods: {
        toggleCollapse() {
            this.isCollapse = !this.isCollapse;
        },
        loginOut() {
            window.sessionStorage.clear();
            this.$router.push("/login");
        },
        // 获取所有的菜单
        async getMenuList() {
            const { data: res } = await this.$http.get("menus");
            if (res.meta.status !== 200)
                return res.meta.msg;
            this.menuList = res.data;
            console.log(res);
        },
        saveNavState(activeStatus){
            window.sessionStorage.setItem('activeStatus',activeStatus)
            this.activeStatus = activeStatus
        }
    },
    components: { RouterView }
}
</script>


<style lang="less" scoped>
.home-container {
    position: fixed;
    height: 100%;
    width: 100%;
    border: none;
}

.el-header {
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    padding-left: 0;
    align-items: center;
    color: #fff;
    font: 20px;
    > div {
        display: flex;
        align-items: center;
        span {
            margin-left: 15px;
        }
    }
}

.el-aside {
    background-color: #333744;
    .el-menu {
        border-right: 0;
    }
}

.el-main {
    background-color: #EAEDF1;
}

.el-btn {
    background-color: rgb(116, 106, 108);
}
.iconfont {
    margin-right: 10px
}
.toggle-btn {
    background-color: #4a5064;
    font-size: 10px;
    line-height: 24px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
}
</style>